package com.gg.bananvideo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.gg.bananvideo.R;
import com.gg.bananvideo.model.ListResponseData;
import com.gg.bananvideo.ui.BVListItem;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author sakura
 * @since 2024/12/17
 */
public class RectclerAdapter extends RecyclerView.Adapter<RectclerAdapter.ViewHolder> {

    private final List<ListResponseData.Data.Vodrows> list;

    public RectclerAdapter(List<ListResponseData.Data.Vodrows> list) {
        this.list = list;
    }

    public interface onItemClickListener {
        void onItemClick(ListResponseData.Data.Vodrows data, int position);
    }


    private onItemClickListener clickListener;

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder viewHolder, int i) {
        ListResponseData.Data.Vodrows data = list.get(i);
        viewHolder.item.setvName(data.getTitle());
        viewHolder.item.setPicUrl(data.getCoverpic());
        StringBuilder tags = new StringBuilder();
        for (ListResponseData.Data.Vodrows.Tags tag : data.getTags()) {
            tags.append(tag.getTagname()).append(" ");
        }
        viewHolder.item.setVDesc(tags.toString());
        viewHolder.setClick(v -> {
            if (clickListener != null) {
                clickListener.onItemClick(data, i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public void setClickListener(onItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        BVListItem item;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            item = itemView.findViewById(R.id.item);
        }

        public void setClick(View.OnClickListener listener) {
            item.setOnClickListener(listener);
        }
    }
}
